#include <algorithm>
#include <cstdio>

const int N = 1e5 + 5, bit = 20;
int n, m, f[bit][N << 1];

struct Segment {
  int a, b;
} seg[N << 1], a[N << 2];

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("testdata.in", "r", stdin);
  freopen("testdata.out", "w", stdout);
#else
  freopen("circular.in", "r", stdin);
  freopen("circular.out", "w", stdout);
#endif
#endif

  scanf("%d%d", &m, &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d%d", &seg[i].a, &seg[i].b);
    if (seg[i].a > seg[i].b) seg[i].b += m;
    seg[i + n].a = seg[i].a + m;
    seg[i + n].b = seg[i].b + m;
  }
  n <<= 1;
  for (int i = 1; i <= n; ++i) {
    a[i].a = seg[i].b;
    a[i].b = i;
    a[i + n].a = seg[i].a;
    a[i + n].b = i + n;
  }
  std::sort(a + 1, a + (n << 1) + 1,
            [](const Segment &lhs, const Segment &rhs) {
              return lhs.a == rhs.a ? lhs.b < rhs.b : lhs.a < rhs.a;
            });
  int lst = -1;
  for (int i = n << 1; i; --i) {
    int id = a[i].b;
    if (id <= n) f[0][id] = lst;
    else {
      id -= n;
      if (lst < 0 || seg[lst].b > seg[id].b) lst = id;
    }
  }
  for (int i = 1; i < bit; ++i) {
    for (int j = 1; j <= n; ++j) {
      if (f[i - 1][j] == -1) f[i][j] = -1;
      f[i][j] = f[i - 1][f[i - 1][j]];
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    int tmp = 0, cur = i;
    for (int j = bit - 1; ~j; --j) {
      if (f[j][cur] > 0 && seg[f[j][cur]].b <= seg[i].a + m) {
        tmp += 1 << j, cur = f[j][cur];
      }
    }
    ans = std::max(ans, tmp + 1);
  }
  printf("%d", ans);
  return 0;
}